<?php

/**
 * This is the model class for table "tbl_type".
 *
 * The followings are the available columns in table 'tbl_type':
 * @property integer $id
 * @property string $name
 * @property string $category
 * @property string $create_date
 * @property string $update_date
 */
class Type extends CActiveRecord
{
    public $alias;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Type the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_type';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required','message'=>'分类名称不能为空'),
            array('screen_name', 'required', 'message'=>'别名名称不能为空'),
			array('name', 'length', 'max'=>128),
            array('category', 'required', 'message'=>'分类类型不能为空'),
            array('name','CategoryExist','on'=>'Insert'),
            array('category','CategoryExist','on'=>'Insert'),
			array('category', 'length', 'max'=>4),
			array('update_date', 'safe'),
            array('is_delete', 'safe'),
            array('is_lock', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, screen_name, category, create_date, update_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'parent_category'=>array(self::BELONGS_TO,'Category',array('category'=>'title'))
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => '分类名称',
			'category' => '类型',
            'screen_name'=>'分类别名',
			'create_date' => '创建日期',
			'update_date' => '更新日期',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
        $criteria->compare('screen_name',$this->name,true);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
	 * 行为操作
	 */
	public function behaviors(){
		return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_date',
                'updateAttribute' => 'update_date',
                'setUpdateOnCreate' => 'true',
            )
		);
	}
    
    public function CategoryExist($arrtibute){
        if($this->name && $this->category){
            
            $criteria=new CDbCriteria();
            $criteria->order = 'name ASC';
            $criteria->condition = 'name = :name AND category = :category';
            $criteria->params = array(':category'=>$this->category,':name'=>$this->name);
            $type = Type::model()->find($criteria);
            
            if(!empty($type) && !empty($type['id'])){
                $this->addError($arrtibute,'添加的类型已经存在');
            }
        }
    }

    /**
     * 获取分类的目录列表
     */
    public function getCategoryList($category){
        $criteria = new CDbCriteria();
        $criteria->condition = 'category=:category';
        $criteria->params = array(':category'=>$category);
        $criteria->order = 'name ASC';
        $categoryList = Type::model()->findAll($criteria);
        return CHtml::listData($categoryList,'id','screen_name');
    }
}
